
import java.util.*;

public class MyBot {
	// This game is a PERFECT AI study case since there are no UNKOWN variables and is close system., 
	// The first try to explore ALL possible future in a recursive tree algorithm but I kept busting the 1 second limit
	//
	// This approach is much simpler where the next 200 turns are predicted according to existing fleets only.
	// (Linear prediction)
	// 
	// Strategy is based almost solely on distance since I noticed that is takes many many turn to reach
	// high growth planet.

	//------------------------------------------------------------------------------------

	static private int nbTurn = 0; 

	//------------------------------------------------------------------------------------

	public static void DoTurn(PlanetWars pw) {

		try{
	
			

			// INIT RainmanPlanet Lists
			List <RainmanPlanet> myPlanetList = ListManager.MyPlanets(pw);
			List <RainmanPlanet> notMyPlanetList = ListManager.notMyPlanets(pw);

			// Loop thru all MYplanets and predict the future
			for (RainmanPlanet p: myPlanetList){
				p.predictFuture(pw.Fleets());			
			}

			// Loop thru NOTmyPlanets and Predict the future
			for (RainmanPlanet p: notMyPlanetList){
				p.predictFuture(pw.Fleets());			
			}


			// Call for help from nearest planets if in danger of loosing in next X turns
			// Start by high GROWTH, they are the most important not to loose
			Collections.sort(myPlanetList, new ComparatorGrowth());
			for(RainmanPlanet p: myPlanetList)
			{
				if(p.inDanger ){
					Util.callForHelp(pw, p, myPlanetList);
				}
			}	

			// Attack best PAYBACK of CLOSEST 8 planets
			Collections.sort(myPlanetList, new ComparatorFreeShips());
			for (RainmanPlanet p: myPlanetList){


				List <RainmanPlanet> targets = Util.findBestPayback(pw,p, 8, notMyPlanetList);

				for( RainmanPlanet target: targets){
					if( p.freeShips > 1 && !target.willBeWon ){
						int shipsSent = Math.min(p.freeShips, target.NumShips()+50);
						
						// simulate sending ship to see if we need to call for help
						
						pw.IssueOrder(p, target, shipsSent);
						p.freeShips -= shipsSent;
						

					}
				}
			}

			nbTurn++;

		}catch(Exception e){

		}

	}
	//------------------------------------------------------------------------------------
	public static void main(String[] args) {

		String line = "";
		String message = "";
		int c;
		try {
			while ((c = System.in.read()) >= 0) {
				switch (c) {
				case '\n':
					if (line.equals("go")) {
						PlanetWars pw = new PlanetWars(message);
						DoTurn(pw);
						pw.FinishTurn();
						message = "";
					} else {
						message += line + "\n";
					}
					line = "";
					break;
				default:
					line += (char)c;
					break;
				}
			}
		} catch (Exception e) {
			// Owned.
		}
	}

	// Used to debug using standard String
	public static void mainUnitTest(String arg) {
		String line = "";
		String message = "";
		int c;
		try {
			for ( int i=0; i<arg.length();i++) {
				c = (int) arg.charAt(i);
				switch (c) {
				case '\n':
					if (line.equals("go")) {
						PlanetWars pw = new PlanetWars(message);
						DoTurn(pw);
						pw.FinishTurn();
						message = "";
					} else {
						message += line + "\n";
					}
					line = "";
					break;
				default:
					line += (char)c;
					break;
				}
			}
		} catch (Exception e) {
			// Owned.
		}

	}		
}

